package com.jiayang

import android.os.Bundle
import android.util.Log
import android.view.View
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.jiayang.mywebview.R
import com.jiayang.mywebview.databinding.ActivityMywebviewBinding

/**
 * @ClassName MyWebViewActivity
 * @Author JIAYANG
 * @Date 2022/4/22 20:38
 * @Email 1354856871@qq.com
 */
const val WEBVIEW_ACTIVITY_URL: String = "url"
const val WEBVIEW_ACTIVITY_TITLE: String = "title"
const val WEBVIEW_ACTIVITY_URL_IS_SHOW_ACTIONBAR: String = "is_show_actionbar"

class MyWebViewActivity : AppCompatActivity() {
    val TAG  = "MyWebActivity"
    lateinit var mBinding : ActivityMywebviewBinding
    var mUrl : String? = null
    var mTitle : String? = null
    var mIsShowActionBar : Boolean = true
    var mCanNativeRefresh : Boolean = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_mywebview)
        mUrl = intent?.extras?.getString(WEBVIEW_ACTIVITY_URL)?:"http:www.baidu.com"
        mTitle = intent?.extras?.getString(WEBVIEW_ACTIVITY_TITLE)?: "WebView"
        mIsShowActionBar = intent?.extras?.getBoolean(WEBVIEW_ACTIVITY_URL_IS_SHOW_ACTIONBAR)?: true
        mCanNativeRefresh = intent?.extras?.getBoolean(WEBVIEW_FRAGMENT_CAN_NATIVE_REFRESH)?: true
        Log.e(TAG,"${intent?.extras?.getString(WEBVIEW_ACTIVITY_URL)}")


        mBinding.tvBar.text = mTitle
        if (!mIsShowActionBar){
            mBinding.actionBar.visibility = View.GONE
        }
        mBinding.btBar.setOnClickListener { this@MyWebViewActivity.finish() }

        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        val fragment : Fragment = MyWebViewFragment.newInstance(intent.getStringExtra(WEBVIEW_FRAGMENT_URL),
            mCanNativeRefresh)
        transaction.replace(R.id.web_view_fragment,fragment).commit()

//        mUrl?.let {
//            mBinding.webView.settings.javaScriptEnabled = true
//            mBinding.webView.loadUrl(it)
//        }
    }

    fun updateTitle(title : String?){
        mBinding.tvBar.text = title
    }
}